package labox.innovation.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import labox.innovation.util.L2Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PvpConfig {
    private static final Logger _log = LoggerFactory.getLogger(PvpConfig.class.getName());

    private static final String PVP_CONFIG_FILE = "./config/pvp.properties";
    // --------------------------------------------------
    // PvP Settings
    // --------------------------------------------------

    /**
     * 玩家每次增加的善恶值的数据
     */
    public static int KARMA_MIN_KARMA;

    /**
     * 玩家的善恶值的上限
     */
    public static int KARMA_MAX_KARMA;

    public static int KARMA_XP_DIVIDER;

    public static int KARMA_LOST_BASE;

    public static boolean KARMA_DROP_GM;

    public static boolean KARMA_AWARD_PK_KILL;

    public static int KARMA_PK_LIMIT;

    public static String KARMA_NONDROPPABLE_PET_ITEMS;

    public static String KARMA_NONDROPPABLE_ITEMS;

    public static int[] KARMA_LIST_NONDROPPABLE_PET_ITEMS;

    public static int[] KARMA_LIST_NONDROPPABLE_ITEMS;
    
    public static int PVP_NORMAL_TIME;

    public static int PVP_PVP_TIME;

    public static void load() {
        _log.info("Loading pvp Configuration Files...", Config.SERVER_ID);
        InputStream is = null;
        try {
            L2Properties pvpSettings = new L2Properties();
            is = new FileInputStream(new File(PVP_CONFIG_FILE));
            pvpSettings.load(is);

            KARMA_MIN_KARMA = Integer.parseInt(pvpSettings.getProperty("MinKarma", "10"));
            KARMA_MAX_KARMA = Integer.parseInt(pvpSettings.getProperty("MaxKarma", "255"));
            KARMA_XP_DIVIDER = Integer.parseInt(pvpSettings.getProperty("XPDivider", "260"));
            KARMA_LOST_BASE = Integer.parseInt(pvpSettings.getProperty("BaseKarmaLost", "0"));
            KARMA_DROP_GM = Boolean.parseBoolean(pvpSettings.getProperty("CanGMDropEquipment", "false"));
            KARMA_AWARD_PK_KILL = Boolean.parseBoolean(pvpSettings.getProperty("AwardPKKillPVPPoint", "true"));
            KARMA_PK_LIMIT = Integer.parseInt(pvpSettings.getProperty("MinimumPKRequiredToDrop", "5"));
            KARMA_NONDROPPABLE_PET_ITEMS = pvpSettings.getProperty("ListOfPetItems", "2375,3500,3501,3502,4422,4423,4424,4425,6648,6649,6650,9882");
            KARMA_NONDROPPABLE_ITEMS = pvpSettings.getProperty("ListOfNonDroppableItems",
                    "57,1147,425,1146,461,10,2368,7,6,2370,2369,6842,6611,6612,6613,6614,6615,6616,6617,6618,6619,6620,6621,7694,8181,5575,7694,9388,9389,9390");

            String[] array = KARMA_NONDROPPABLE_PET_ITEMS.split(",");
            KARMA_LIST_NONDROPPABLE_PET_ITEMS = new int[array.length];

            for (int i = 0; i < array.length; i++)
                KARMA_LIST_NONDROPPABLE_PET_ITEMS[i] = Integer.parseInt(array[i]);

            array = KARMA_NONDROPPABLE_ITEMS.split(",");
            KARMA_LIST_NONDROPPABLE_ITEMS = new int[array.length];

            for (int i = 0; i < array.length; i++)
                KARMA_LIST_NONDROPPABLE_ITEMS[i] = Integer.parseInt(array[i]);

            // sorting so binarySearch can be used later
            Arrays.sort(KARMA_LIST_NONDROPPABLE_PET_ITEMS);
            Arrays.sort(KARMA_LIST_NONDROPPABLE_ITEMS);

            PVP_NORMAL_TIME = Integer.parseInt(pvpSettings.getProperty("PvPVsNormalTime", "120000"));
            PVP_PVP_TIME = Integer.parseInt(pvpSettings.getProperty("PvPVsPvPTime", "60000"));

        } catch (Exception e) {
            _log.error("Failed to Load " + PVP_CONFIG_FILE + " File.", e);
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                _log.error("InputStream close occured IOException");
            }
        }
    }
}
